﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

using $prjsn$.Common;
using $prjsn$.Common.Validation;
using $prjsn$.Common.Data.Services;
using $prjsn$.Entity.Services;

namespace $safeprojectname$.Services
{
    public class LangFilterValidator : FilterValidatorBase<Lang, LangFilter, int>
    {
        protected override void Init(RuleCollection<LangFilter> rules) { base.Init(rules); }
    }
    public class LangValidator : EntityValidatorBase<Lang, int, ILangDao>
    {
        protected override void Init(RuleCollection<Lang> rules)
        {
            // unique key and area
            rules.For(entity => entity.LanguageName).RequireLengthBetween(2, 15)
                .BreakOnError();

            rules.Assure(IsLanguageNameValid);

            rules.IsUnique(entity => Dao.Find(entity.ID, new LangFilter { LanguageNameSearch = entity.LanguageName }), "LanguageName");
        }

        #region Check

        protected virtual ValidationResult IsLanguageNameValid(Lang entity, out ValidationMessage message)
        {
            message = null;
            var languageExists = CultureInfo.GetCultures(CultureTypes.AllCultures)
                .ToList()
                .Any(c => c.Name.Equals(entity.LanguageName, StringComparison.Ordinal));
            if (languageExists)
            {
                return ValidationResult.IsValid;
            }

            message = new ValidationMessage(Str.Messages.LanguageNotExists1)
            {
                entity.LanguageName,
            };
            return ValidationResult.Error;
        }
        #endregion Check
    }
}